//
//  AppDelegate.m
//  BeanTrip
//
//  Created by dlios on 15-6-29.
//  Copyright (c) 2015年 PandaGroup. All rights reserved.
//

#import "AppDelegate.h"
#import "FirstViewController.h"
#import "SecondViewController.h"
#import "ThirdViewController.h"
#import "FourthViewController.h"
#import "LeftViewController.h"
#import "UMSocial.h"
/**
 *  宏定义 宽和高
 *
 */
#define WIDTH ([UIScreen mainScreen].bounds.size.width/375)
#define HEIGH ([UIScreen mainScreen].bounds.size.height/667)

@interface AppDelegate ()
<UITabBarControllerDelegate,UIScrollViewDelegate>
/**
 *  设置tabBar属性
 */
@property (nonatomic, retain) UITabBarController *tabBar;
/**
 *  页面视图
 */
@property (nonatomic, retain) UIPageControl *page;
@end
@implementation AppDelegate

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    // Override point for customization after application launch.
    self.window.backgroundColor = [UIColor whiteColor];
    [self.window makeKeyAndVisible];
    
    //友盟分享设置appkey
    [UMSocialData setAppKey:@"55a3a50767e58eded90035da"];

    /**
     *  添加图片
     */
    UIImage *recommend=[UIImage imageNamed:@"BeanTrip-tuijian.png"];
    UIImage *classify=[UIImage imageNamed:@"BeanTrip-fenlei.png"];
    UIImage *specialOffer=[UIImage imageNamed:@"BeanTrip-tejia.png"];
    UIImage *individual=[UIImage imageNamed:@"BeanTrip-xiugai.png"];
    /**
     * 背景图片
     */
    UIImage *beanTripBackgroundImage=[UIImage imageNamed:@"BeanTripB.png"];
    /**
     创建各个tabBar
     */
    FirstViewController *fistVC=[[FirstViewController alloc] init];
    UINavigationController *recommendVC=[[UINavigationController alloc] initWithRootViewController:fistVC];
    recommendVC.tabBarItem=[[UITabBarItem alloc] initWithTitle:@"豆游推荐" image:recommend tag:1001];
    [recommendVC.navigationBar setBackgroundImage:beanTripBackgroundImage forBarMetrics:UIBarMetricsDefault];
    
    SecondViewController *secondVC=[[SecondViewController alloc] init];
    UINavigationController *classifyVC=[[UINavigationController alloc] initWithRootViewController:secondVC];
    classifyVC.tabBarItem=[[UITabBarItem alloc] initWithTitle:@"景点分类" image:classify tag:1002];
    [classifyVC.navigationBar setBackgroundImage:beanTripBackgroundImage forBarMetrics:UIBarMetricsDefault];
    
    ThirdViewController *thirdVC=[[ThirdViewController alloc] init];
    UINavigationController *specialOfferVC=[[UINavigationController alloc] initWithRootViewController:thirdVC];
    specialOfferVC.tabBarItem=[[UITabBarItem alloc] initWithTitle:@"景点特价" image:specialOffer tag:1003];
    [specialOfferVC.navigationBar setBackgroundImage:beanTripBackgroundImage forBarMetrics:UIBarMetricsDefault];
    
    FourthViewController *fourthVC=[[FourthViewController alloc] init];
    UINavigationController *individualVC=[[UINavigationController alloc] initWithRootViewController:fourthVC];
    individualVC.tabBarItem=[[UITabBarItem alloc] initWithTitle:@"更多" image:individual tag:1004];
    [individualVC.navigationBar setBackgroundImage:beanTripBackgroundImage forBarMetrics:UIBarMetricsDefault];

    /**
     创建一个UITabBarController对象
     */
    self.tabBar= [[UITabBarController alloc] init];
    
    /**
     * 用table对所有naVC进行管理
     */
    self.tabBar.viewControllers = [NSArray arrayWithObjects:recommendVC, classifyVC, specialOfferVC, individualVC, nil];
    
    /**
     * 设置不透明
     */
    self.tabBar.tabBar.translucent = NO;
    // self.window.rootViewController = self.tabBar;
    
    /**
     * 设置被选中的颜色
     */
    self.tabBar.tabBar.tintColor=[UIColor whiteColor];
    /**
     * 最下面导航栏的颜色
     */
    // self.tabBar.tabBar.barTintColor=[UIColor blackColor];
    
    /**
     * 设置tabBar的背景图片
     */
    [self.tabBar.tabBar setBackgroundImage:[UIImage imageNamed:@"BeanTripB.png"]];
 
    /**
     *  签订协议
     */
    self.tabBar.delegate=self;
    
    /**
     * tabbar 不选中时的颜色
     */
    [[UITabBarItem appearance] setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIColor grayColor], NSForegroundColorAttributeName,nil] forState:UIControlStateNormal];
    /**
     *  tabbar 选中时的颜色
     */
    [[UITabBarItem appearance] setTitleTextAttributes: [NSDictionary dictionaryWithObjectsAndKeys:[UIColor whiteColor],NSForegroundColorAttributeName,nil]forState:UIControlStateSelected];
    
    
    LeftViewController *leftVC = [[LeftViewController alloc] init];
    self.sideMenuVC = [[RESideMenu alloc] initWithContentViewController:self.tabBar leftMenuViewController:leftVC rightMenuViewController:nil];
    self.sideMenuVC.backgroundImage = [UIImage imageNamed:@"Stars"];
    self.sideMenuVC.menuPreferredStatusBarStyle = 1;
    self.sideMenuVC.delegate =self;
    self.sideMenuVC.contentViewShadowColor = [UIColor blackColor];
    self.sideMenuVC.contentViewShadowOffset = CGSizeMake(0, 0);
    self.sideMenuVC.contentViewShadowOpacity = 0.6;
    self.sideMenuVC.contentViewShadowRadius = 12;
    self.sideMenuVC.contentViewShadowEnabled = YES;

    if (![[NSUserDefaults standardUserDefaults] boolForKey:@"everLaunched"]) {
        [[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"everLaunched"];
        [[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"firstLaunch"];

    
    /**
     scrollView处理
     */
    UIScrollView *myScroll = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, 375*WIDTH, 667*HEIGH)];
    myScroll.contentSize = CGSizeMake(375*WIDTH * 3, 667*HEIGH);
    for (int i = 0; i < 3; i++) {
            UIImageView *myImage = [[UIImageView alloc] initWithFrame:CGRectMake(375*WIDTH * i, 0, 375*WIDTH, 667*HEIGH)];
    
        myImage.image = [UIImage imageNamed:[NSString stringWithFormat:@"image%d.jpg", i + 1]];
            [myScroll addSubview:myImage];
    
        }
        myScroll.pagingEnabled = YES;
        myScroll.showsHorizontalScrollIndicator = NO;
        [self.window addSubview:myScroll];
    /**
     *  最后一个页面设置一个button
     *
     */
        UIButton *button = [UIButton buttonWithType:(UIButtonTypeCustom)];
        [myScroll addSubview:button];
        button.frame = CGRectMake(self.window.frame.size.width * 2 + self.window.frame.size.width / 2 - 80 * WIDTH, self.window.frame.size.height - 92 * HEIGH, 160, 50);
        [button addTarget:self action:@selector(click) forControlEvents:(UIControlEventTouchUpInside)];
    
    /**
     *  签订协议
     */
        myScroll.delegate = self;
    /**
     页面处理

     */
        self.page = [[UIPageControl alloc] initWithFrame:CGRectMake(0, 620, 375*WIDTH, 30)];
        self.page.numberOfPages = 3;
        self.page.currentPage = 0;
        self.page.backgroundColor = [UIColor clearColor];
        self.page.currentPageIndicatorTintColor = [UIColor cyanColor];
        self.page.pageIndicatorTintColor = [UIColor whiteColor];
        [self.window addSubview:self.page];
    }
        else{
            
            [[NSUserDefaults standardUserDefaults] setBool:NO forKey:@"firstLaunch"];
        
            /**
             *  添加图片
             */
            UIImage *recommend=[UIImage imageNamed:@"BeanTrip-tuijian.png"];
            UIImage *classify=[UIImage imageNamed:@"BeanTrip-fenlei.png"];
            UIImage *specialOffer=[UIImage imageNamed:@"BeanTrip-tejia.png"];
            UIImage *individual=[UIImage imageNamed:@"BeanTrip-xiugai.png"];
            /**
             * 背景图片
             */
            UIImage *beanTripBackgroundImage=[UIImage imageNamed:@"BeanTripB.png"];
            /**
             创建各个tabBar
             */
            FirstViewController *fistVC=[[FirstViewController alloc] init];
            UINavigationController *recommendVC=[[UINavigationController alloc] initWithRootViewController:fistVC];
            recommendVC.tabBarItem=[[UITabBarItem alloc] initWithTitle:@"豆游推荐" image:recommend tag:1001];
            [recommendVC.navigationBar setBackgroundImage:beanTripBackgroundImage forBarMetrics:UIBarMetricsDefault];
            
            SecondViewController *secondVC=[[SecondViewController alloc] init];
            UINavigationController *classifyVC=[[UINavigationController alloc] initWithRootViewController:secondVC];
            classifyVC.tabBarItem=[[UITabBarItem alloc] initWithTitle:@"景点分类" image:classify tag:1002];
            [classifyVC.navigationBar setBackgroundImage:beanTripBackgroundImage forBarMetrics:UIBarMetricsDefault];
            
            ThirdViewController *thirdVC=[[ThirdViewController alloc] init];
            UINavigationController *specialOfferVC=[[UINavigationController alloc] initWithRootViewController:thirdVC];
            specialOfferVC.tabBarItem=[[UITabBarItem alloc] initWithTitle:@"景点特价" image:specialOffer tag:1003];
            [specialOfferVC.navigationBar setBackgroundImage:beanTripBackgroundImage forBarMetrics:UIBarMetricsDefault];
            
            FourthViewController *fourthVC=[[FourthViewController alloc] init];
            UINavigationController *individualVC=[[UINavigationController alloc] initWithRootViewController:fourthVC];
            individualVC.tabBarItem=[[UITabBarItem alloc] initWithTitle:@"更多" image:individual tag:1004];
            [individualVC.navigationBar setBackgroundImage:beanTripBackgroundImage forBarMetrics:UIBarMetricsDefault];
            
   
            /**
             创建一个UITabBarController对象
             */
            self.tabBar= [[UITabBarController alloc] init];
            
            /**
             * 用table对所有naVC进行管理
             */
            self.tabBar.viewControllers = [NSArray arrayWithObjects:recommendVC, classifyVC, specialOfferVC, individualVC, nil];
            /**
             * 设置不透明
             */
            self.tabBar.tabBar.translucent = NO;
            // self.window.rootViewController = self.tabBar;
            
            /**
             * 设置被选中的颜色
             */
            self.tabBar.tabBar.tintColor=[UIColor whiteColor];
            
            /**
             * 最下面导航栏的颜色
             */
            // self.tabBar.tabBar.barTintColor=[UIColor blackColor];
            
            /**
             * 设置tabBar的背景图片
             */
            [self.tabBar.tabBar setBackgroundImage:[UIImage imageNamed:@"BeanTripB.png"]];
            /**
             *  签订协议
             */
            self.tabBar.delegate=self;
            /**
             * tabbar 不选中时的颜色
             */
            [[UITabBarItem appearance] setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIColor grayColor], NSForegroundColorAttributeName,nil] forState:UIControlStateNormal];
            /**
             *  tabbar 选中时的颜色
             */
            [[UITabBarItem appearance] setTitleTextAttributes: [NSDictionary dictionaryWithObjectsAndKeys:[UIColor whiteColor],NSForegroundColorAttributeName,nil]forState:UIControlStateSelected];
            LeftViewController *leftVC = [[LeftViewController alloc] init];
            self.sideMenuVC = [[RESideMenu alloc] initWithContentViewController:self.tabBar leftMenuViewController:leftVC rightMenuViewController:nil];
            self.sideMenuVC.backgroundImage = [UIImage imageNamed:@"Stars"];
            self.sideMenuVC.menuPreferredStatusBarStyle = 1;
            self.sideMenuVC.delegate =self;
            self.sideMenuVC.contentViewShadowColor = [UIColor blackColor];
            self.sideMenuVC.contentViewShadowOffset = CGSizeMake(0, 0);
            self.sideMenuVC.contentViewShadowOpacity = 0.6;
            self.sideMenuVC.contentViewShadowRadius = 12;
            self.sideMenuVC.contentViewShadowEnabled = YES;
            self.window.rootViewController = self.sideMenuVC;
}
    
    return YES;
}

/**
 *  button的点击方法,处理跳转
 */
- (void)click
{
    self.window.rootViewController = self.sideMenuVC;

}

/**
 *  页面移动处理
 */
- (void)scrollViewDidScroll:(UIScrollView *)scrollView

{
    self.page.currentPage = scrollView.contentOffset.x / scrollView.window.frame.size.width;
}

- (void)applicationWillResignActive:(UIApplication *)application {
    // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
    // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
}

- (void)applicationDidEnterBackground:(UIApplication *)application {
    // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
    // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}

- (void)applicationWillEnterForeground:(UIApplication *)application {
    // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
}

- (void)applicationDidBecomeActive:(UIApplication *)application {
    // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
}

- (void)applicationWillTerminate:(UIApplication *)application {
    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}

@end
